<?php

use app\models\Agent;
use app\models\User;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use yii\widgets\Pjax;
use app\models\AgentPackage;
/**
 * @var $this yii\web\View
 * @var $model app\models\CardDispatchForm
 */

$this->title = '分配卡';
$this->params['breadcrumbs'][] = '物联网卡管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php $form = ActiveForm::begin();?>
<fieldset>
    <legend><label><input type="radio" name="MultiOrder[source]" value="range" checked="checked"> 设定起止卡号</label></legend>
    <?php echo $form->field($model, 'iccid_start')->hint('开始号码和结束号码必须同时填写。');?>
    <?php echo $form->field($model, 'iccid_end')->hint('开始号码结束号码和ICCID列表必须至少填写一项。');?>
</fieldset>
<?php echo $form->field($model, 'iccid_list')->textarea(['style' => 'min-height:200px;'])->label('<label><input type="radio" name="MultiOrder[source]" value="iccid"> 直接填写ICCID列表</label>');?>
<?php echo $form->field($model, 'package_id')->dropDownList(ArrayHelper::map(AgentPackage::find()->andWhere(['agent_id' => $agent])->all(), 'id', 'name'), ['prompt' => '选择套餐']);?>
<?php echo $form->field($model,'price');?>
<?php echo $form->field($model,'number');?>
<?php echo $form->field($model,'term_length');?>
<?php echo $form->field($model,'total_price')->textInput(['readonly'=>'true']);?>
<div class="form-group">
    <div class="col-lg-offset-1 col-lg-11">
        <button type="button" class="btn btn-default" onclick="window.history.go(-1);"><i class="ace-icon fa fa-arrow-left bigger-110"></i>返回</button>
        <button class="btn btn-primary"><i class="ace-icon fa fa-check bigger-110"></i>保存</button>
        <button type="reset" class="btn btn-warning"><i class="ace-icon fa fa-undo bigger-110"></i>重置</button>
    </div>
</div>
<?php $form->end();?>
<script>
    function pageInit() {

        $('[name="MultiOrder[package_id]"]').change(function () {
            var id = $(this).val();
            $.ajax({
                url:"packageprice?id="+id,    //请求的url地址
                dataType:"json",   //返回格式为json
                async:true,
                type:"get",
                success:function(req){
                    if(req.code==1){
                        var number = $('[name="MultiOrder[number]"]').val();
                        
                        $('[name="MultiOrder[price]"]').attr("value",req.data.sale_price);

                    }
                }
            });

        })
        $('[name="MultiOrder[price]"]').change(function(){
            var price = $(this).val();
            var number = $('[name="MultiOrder[number]"]').val();
            if(number==''){
                $('[name="MultiOrder[total_price]"]').val(price);
            }else{
                $('[name="MultiOrder[total_price]"]').val(accMul(price,number));
            }
        });
        $('[name="MultiOrder[number]"]').keyup(function(){
            var number = $(this).val();
            var price = $('[name="MultiOrder[price]"]').val();
            if(price==''){
                $('[name="MultiOrder[total_price]"]').val(price);
            }else{
                $('[name="MultiOrder[total_price]"]').val(accMul(price,number));
            }
        });
        $('[name="MultiOrder[term_length]"]').keyup(function(){
            var term_length = $(this).val();
            var number = $(this).val();
            var price = $('[name="MultiOrder[price]"]').val();
            if(price==''){
                $('[name="MultiOrder[total_price]"]').val(price);
            }else{
                $('[name="MultiOrder[total_price]"]').val(accMul(price,number));
            }
        });

        //处理js乘法精度方法
        function accMul(arg1,arg2){
            var m=0,s1=arg1.toString(),s2=arg2.toString();
            try{m+=s1.split(".")[1].length}catch(e){}
            try{m+=s2.split(".")[1].length}catch(e){}
            return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
        }

    }

</script>
